package gui.image;

import gui.ImageListShower;
import gui.popupmenu.AlbumImagePanelPopup;
import gui.popupmenu.FolderImagePanelPopup;
import gui.popupmenu.ImagePanelPopup;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.imageio.ImageIO;
import qviewer.ServiceManager;

/**
 *
 * @author Quân
 */
public class ImagePanel extends javax.swing.JPanel implements Cloneable {

    private File file;
    private BufferedImage image;

    /**
     * Creates new form ImagePanel
     */
    public ImagePanel(File file, double scale) throws IOException {
        initComponents();
        setImage(file);
        this.scale = scale;
    }

    public ImagePanel(File file) throws IOException {
        initComponents();
        setImage(file);
    }

    private void setImage(File file) throws IOException {
        this.file = file;
        this.image = ImageIO.read(file);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        int w = getWidth();
        int h = getHeight();
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        int max = Math.max(imageHeight, imageWidth);
        if (max <= w) {
            scale = 1;
        } else {
            scale = (double) w / max;
        }

        double x = (w - scale * imageWidth) / 2;
        double y = (h - scale * imageHeight) / 2;
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
        at.scale(scale, scale);
        g2.drawRenderedImage(image, at);
    }

    @Override
    public ImagePanel clone() {
        try {
            return (ImagePanel) super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), 3));
        setMinimumSize(new java.awt.Dimension(150, 150));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 144, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 144, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        ImageList imageList = (ImageList) this.getParent().getParent();
        switch (evt.getButton()) {
            case MouseEvent.BUTTON1:
                ImageListShower shower = ServiceManager.getInstance().getShower();
                shower.setCurrent(imageList.getjPanelImage().getComponentZOrder(this));

                List<BufferedImage> list = new LinkedList<>();
                int count = imageList.getjPanelImage().getComponentCount();
                for (int i = 0; i < count; i++) {
                    ImagePanel panel = (ImagePanel) imageList
                            .getjPanelImage().getComponent(i);
                    list.add(panel.getImage());
                }
                shower.setImages(list);
                shower.setFiles(imageList.getFiles());
                shower.setVisible(true);
                break;
            case MouseEvent.BUTTON3:
                ImagePanel current = imageList.getSelected();
                if (current != null) {
                    current.setBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("Panel.background"), 3));
                }
                setBorder(javax.swing.BorderFactory.createLineBorder(javax.swing.UIManager.getDefaults().getColor("List.selectionBackground"), 3));
                ImagePanelPopup menu = null;
                if (imageList instanceof FolderImageList) {
                    menu = new FolderImagePanelPopup(this);
                } else if (imageList instanceof AlbumImageList) {
                    menu = new AlbumImagePanelPopup(this);
                }
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
                imageList.setSelected(this);
                break;
        }
    }//GEN-LAST:event_formMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public double getScale() {
        return scale;
    }

    public void setScale(double scale) {
        this.scale = scale;
    }
    private double scale;
}
